// 主界面
;define(function(require, exports, module) {
    'use strict';
    var base = require(CONFIG.BasePath + "js/enterprise/base");
	var register_css = require("./register_setPsw.css");
	var mobile;
	var authenticode;
	var registerUuid;
	var flag = false;
	var router = require("../hash/pageRouter");
	var tools = require("tools");
    var init = function(page){
    	history.go(1);
    	mobile=page.params.mobile||base.getParam("mobile");
    	authenticode=page.params.authenticode||base.getParam("authenticode");
    	registerUuid=page.params.registerUuid||base.getParam("registerUuid");
    	initEvent();
    };
    
	var initEvent = function(){
		
		$("#backRegistMain").unbind("click").click(function(){
			location.href="../register/register_main.html";
		});
		
		$("#registerSubmit").unbind("click").click(function(){
			if($("#registerPassword").val()==""||$("#registerPassword").val()==null){
				base.alert(2,"请输入密码！");
				flag=false;
			}else if($("#registerPasswordConfirm").val()==""||$("#registerPasswordConfirm").val()==null){
				base.alert(2,"请确认输入的密码！");
				flag=false;
			}else if($("#registerPasswordConfirm").val()!=$("#registerPassword").val()){
				base.alert(2,"两次输入的密码不同！");
				flag=false;
			}
//			else if($("#acceptProtocol").prop("checked")!=true){
//				base.alert(2,"请认真阅读协议并确认！");
//				flag=false;
//			}
			else{
				flag=true
			}
			if(flag){
				tools.protectDoubleClick();
				register();
			}
		});
    };
    
    //注册-提交设置密码
    var register = function() {
    	var password=$("#registerPassword").val();
    	base.fnAjax({
            action: "registerEnterprisePassword.json",
            nocode: true,
            oData: {
                mobile: mobile,
                registerUuid: registerUuid,
                authenticode: authenticode,
                password:password
            }, //传递参数
            fnSuccess: function(data) {
            	tools.cancelProtectDoubleClick();
                if (data.status == 200) {
                	login(mobile, password);
                } else {
					flag = false;
					base.alert(3,data.message);
					flag = false;
                }
            }
        });
    };
    
    var login = function (mUsername, mPassword){
    	base.fnAjax({
			action: "userNameLogin.json",
			async: true,
			nocode: true,
			oData: {
				username: mUsername,
				password: mPassword,
				appKey: "FINANCE-ENTERPRISE-WEB"
			}, //传递参数
			fnSuccess: function(data) {
				tools.cancelProtectDoubleClick();
				if (data.status == 200) {
					base.saveData("sid", data.datas[0].sessionUuid); //存sessionUuid
					base.saveData("appKey", "FINANCE-ENTERPRISE-WEB"); //存appkey
					base.saveData("tenantId", data.datas[0].tenantId); //存accountId
					router.route("register/register_success.html");
				} else {
				}
			}
		});
    };
    
    return {
        init:init
    };
});